import typing as t

import strawberry
from app import my_hash
from app.api.graphql.base import TortoiseObjectType
from app.common.auth import Login
from app.models import User as UserModel
from app.models.user import gender_dict
from strawberry.types import Info


@strawberry.type
class DisplayUser(TortoiseObjectType):
    class Meta:
        model = UserModel
        name_mapping = {"display_name": ["username", "phone_number"]}

    @strawberry.field
    def auto_id(self: UserModel) -> str:
        return my_hash.encode(self.auto_id)

    @strawberry.field
    def display_name(self: UserModel) -> str:
        return self.username or self.phone_number

    @strawberry.field
    def gender(self: UserModel) -> str:
        return gender_dict[self.gender]

    description: t.Optional[str]
    avatar_url: t.Optional[str]
    created_at: str
    updated_at: str


@strawberry.type
class CommunityUser(DisplayUser):
    is_following: bool


@strawberry.field(permission_classes=[Login])
async def users_field(info: Info) -> t.List[DisplayUser]:
    users = await UserModel.all().only(*DisplayUser.load_model_field(info))
    return users
